import areaData from '@/assets/region/data'

export function getRegion(code) {
  const region = {
    province: null,
    city: null,
    area: null
  }
  const province = code.substring(0, 2) + '0000'
  const city = code.substring(0, 4) + '00'
  if (areaData[province]) {
    region.province = areaData[province].name
  }
  if (areaData[province] && areaData[province].child[city]) {
    region.city = areaData[province].child[city].name
    region.area = areaData[province].child[city].child[code]
  }
  return region
}

export function packRegionCode(code){
  const region = {
    province: 0,
    city: 0,
    area: 0,
    street: 0,
    community: 0
  }
  if (code.length > 1){
    region.province = Number(code.substring(0, 2) + '0000')
  }
  if (code.length > 3 && code.substring(2, 4) !== '00'){
    region.city = Number(code.substring(0, 4) + '00')
  }

  if (code.length > 5 && code.substring(4, 6) !== '00'){
    region.area = Number(code.substring(0, 6))
  }
  if (code.length > 9 && code.substring(6, 9) !== '000'){
    region.street = Number(code.substring(0, 9) + '000')
  }
  if (code.length > 11 && code.substring(9, 12) !== '000'){
    region.community = Number(code.substring(0, 12))
  }
  return region
}
